package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.QuestionAndAnswerRabbitMqInfo;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 *
 * @author xuanqicheng
 * @create 2018-24-11:58
 */
@Component
@Slf4j
public class PaySuccessRabbitMq {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 将订单信息放入到队列中
     * @param orderId
     */

    /**
     *  将订单信息放入到队列中
     * @param orderId
     */
    public void setPaySuccessInfo(String orderId) {
        String delayTime = "30000";
        if(!StringUtils.isEmpty(orderId)){
            amqpTemplate.convertAndSend(QuestionAndAnswerRabbitMqInfo.DELAY_EXHANGE,
                    QuestionAndAnswerRabbitMqInfo.DELAY_PAY_ROUTING_KEY, orderId, new MessagePostProcessor() {
                        @Override
                        public Message postProcessMessage(Message message) throws AmqpException {
                            message.getMessageProperties().setExpiration(delayTime);
                            return message;
                        }
                    });
        }
    }

    /**
     * 获取抽奖次数
     * @param userId
     * @param contentType 1 金币，2：vip
     */
    public void setNewYearDayActivity(String userId,int contentType){

        if(!StringUtils.isEmpty(userId)){
            log.info("获取MQ充值人信息：,金币类型：{}",userId,contentType);
            JSONObject json = new JSONObject();
            json.put("userId",userId);
            json.put("contentType",contentType);
            amqpTemplate.convertAndSend("NEW_YEAR_DAY_EXCHANGE","NEW_YEAR_DAY_PAY",json.toJSONString());
        }

    }

}
